package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.receiptplace.NoSuchReceiptPlaceException;
import com.xinzhitong.www.exception.receiptplace.ReceiptPlaceEmptyException;
import com.xinzhitong.www.exception.receiptplace.ReceiptPlaceIncompleteException;
import com.xinzhitong.www.pojo.ReceiptPlace;
import com.xinzhitong.www.service.ReceiptPlaceService;
import com.xinzhitong.www.utils.Message;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 收货地址控制器
 *
 * @author 徐鑫
 */
@CrossOrigin
@RestController
@RequestMapping("receiptPlace")
public class ReceiptPlaceController {

    @Resource
    ReceiptPlaceService receiptPlaceService;

    /**
     * 获取收货地址
     *
     * @return 收货地址列表
     */
    @RequestMapping(value = "allReceiptPlace", method = RequestMethod.GET)
    public Message allReceiptPlace() {
        String msg;
        try {
            return Message.createSuc(receiptPlaceService.getUsersReceiptPlaces());
        } catch (ReceiptPlaceEmptyException e) {
            e.printStackTrace();
            msg = "收货地址为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 添加一个收货地址
     *
     * @param receiptPlace 收货地址
     * @return 响应信息
     */
    @RequestMapping(value = "aReceiptPlace", method = RequestMethod.PUT)
    public Message aReceiptPlace(
            @RequestBody ReceiptPlace receiptPlace
    ) {
        String msg = "添加收货地址成功";
        try {
            receiptPlaceService.addAReceiptPlace(receiptPlace);
            return Message.createSuc(msg);
        } catch (ReceiptPlaceIncompleteException e) {
            e.printStackTrace();
            msg = "收货地址信息不完整";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取一个收货地址
     *
     * @param id 收货地址id
     * @return 收货地址信息
     */
    @RequestMapping(value = "theReceiptPlace", method = RequestMethod.GET)
    public Message theReceiptPlace(
            @RequestParam("id") Integer id
    ) {
        String msg;
        try {
            return Message.createSuc(receiptPlaceService.getTheReceiptPlaceOf(id));
        } catch (NoSuchReceiptPlaceException e) {
            e.printStackTrace();
            msg = "收货地址不存在或系统正忙";
        }
        return Message.createErr(msg);
    }

    /**
     * 编辑一个收货地址
     *
     * @param receiptPlace 收货地址
     * @return 响应信息
     */
    @RequestMapping(value = "theReceiptPlaceById", method = RequestMethod.POST)
    public Message theReceiptPlaceById(
            @RequestBody ReceiptPlace receiptPlace
    ) {
        String msg = "编辑收货地址成功";
        try {
            receiptPlaceService.editTheReceiptPlace(receiptPlace);
            return Message.createSuc(msg);
        } catch (ReceiptPlaceIncompleteException e) {
            e.printStackTrace();
            msg = "收货地址信息不完整";
        } catch (NoSuchReceiptPlaceException e) {
            e.printStackTrace();
            msg = "收货地址不存在或系统正忙";
        }
        return Message.createErr(msg);
    }

    /**
     * 删除指定的收货地址
     *
     * @param ids 收货地址id列表
     * @return 响应信息
     */
    @RequestMapping(value = "theReceiptPlaceSwapper", method = RequestMethod.DELETE)
    public Message theReceiptPlaceSwapper(
            @RequestBody Integer[] ids
    ) {
        String msg = "删除成功";
        try {
            receiptPlaceService.deleteTheReceiptPlaceIn(ids);
            return Message.createSuc(msg);
        } catch (ReceiptPlaceEmptyException e) {
            e.printStackTrace();
            msg = "请选择要删除的收货地址";
        }
        return Message.createErr(msg);
    }

}
